package com.lee.forum.utils;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lee.forum.common.ResultMessage;

/**
 * 这个类是用于封装消息的工具类（用于服务端向客户端推送消息时，进行消息的封装）
 * {"isSystem":true,"isDead":"false","fromName":null,"message":"张三" }
 * {"isSystem":false,"fromName":"张三","message":"你好" }
 */
public class MessageUtils {
    public static String getMessage(boolean isSystem,boolean isDead ,String fromName,Object message) throws JsonProcessingException {
        // 0.初始化消息对象
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setSystem(isSystem);
        resultMessage.setDead(isDead);
        resultMessage.setMessage(message);
        // 1.判断是否为系统消息（广播消息）
        if (fromName != null) {
            resultMessage.setFromName(fromName);
        }
        // 2.序列化为JSON字符串返回
        ObjectMapper objectMapper = new ObjectMapper();
        String result = objectMapper.writeValueAsString(resultMessage);
        return result;
    }

}
